package tcp;

import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.msg.WriteMaskRegisterRequest;
import com.serotonin.modbus4j.msg.WriteMaskRegisterResponse;

public class twriteMaskRegister {
	public static String ipAdress;
	public static int ipPort;
	public static int slaveId;
	public static int offset; 
	public static int and;
	public static int or;
	public twriteMaskRegister(String ipAdress1, int ipPort1, int slaveId1, int offset1, int and1, int or1)
	{
		ipAdress = ipAdress1; 
		ipPort = ipPort1;
		 slaveId = slaveId1;
		 offset = offset1;
		 and  = and1;
		 or  = or1;
	};
	public static void remain1(){
		IpParameters ipParameters = new IpParameters();
        ipParameters.setHost(ipAdress);
        ipParameters.setPort(ipPort);
		ModbusFactory modbusFactory = new ModbusFactory();

		ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, false);
        try {
			master.init();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        writeMaskRegisterTest(master, slaveId, offset, and, or);
	};
	
    public static void writeMaskRegisterTest(ModbusMaster master, int slaveId3, int offset3, int and3, int or3) {
        try {
            WriteMaskRegisterRequest request = new WriteMaskRegisterRequest(slaveId3, offset3, and3, or3);
            WriteMaskRegisterResponse response = (WriteMaskRegisterResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println("Success");
        }
        catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }

}
